package com.travelms.service.commonuserpage;

import com.travelms.controller.CommonUserPage;
import com.travelms.dao.DBFind;
import com.travelms.util.DataEffect;

import java.io.IOException;
import java.sql.SQLException;
import java.util.Scanner;
import java.util.Stack;

public class MopdifyOneself {
    private DBFind dbFind = new DBFind();

    public MopdifyOneself(String username) throws SQLException, IOException, InterruptedException {
        Stack<Integer> stack = new Stack<>();
        Scanner sc = new Scanner(System.in);
        int current = 0;
        stack.push(current);

        while (true) {
            if(CommonUserPage.passwordStatus == 1){
                break;
            }
            if (current == 0) {
                System.out.println("1.密码");
                System.out.println("2.找回凭证");
                System.out.println("3.年龄");
                System.out.println("4.性别");
                System.out.println("5.退出");
            }
            while (true) {
                System.out.println("亲输入:");
                String s = sc.nextLine();
                if (!DataEffect.isNumber(s)) {//输入效验
                    System.out.println("输入错误");
                } else if (!(s.compareTo("0") > 0 && s.compareTo("6") < 0)) {
                    System.out.println("输入错误");
                } else {
                    current = Integer.parseInt(s);//记录输入的层级
                    stack.push(current);//压入栈
                    break;
                }
            }
            if (current == 5) {

                //为3则退出
                break;
            } else if (current == 1) {
                //修改密码


                while (true) {
                    System.out.println("输入您要修改的密码：");
                    String newPsw = sc.nextLine();
                    if (!(DataEffect.checkPasswordRule(newPsw))){
                        System.out.println("新密码不符合规范");
                        continue;
                    }
                    if (dbFind.modifyPsw(username,newPsw)) {
                        System.out.println("修改成功,修改后的信息为");
                        CommonUserPage.passwordStatus = 1;
                        System.out.println("当前登录状态已经失效,请重新登录");
                        System.out.println("3秒后自动退出");
                        System.out.println("3");
                        Thread.sleep(1000);
                        System.out.println("2");
                        Thread.sleep(1000);
                        System.out.println("1");
                        Thread.sleep(1000);
                        break;
                    }else {
                        System.out.println("修改失败");
                        break;
                    }

                }
                stack.pop();
                current = stack.peek();

            }else if (current == 2) {
                //修改找回凭证
                while (true) {
                    System.out.println("输入您要修改的找回凭证：");
                    String newKey = sc.nextLine();
                    if (newKey.isEmpty()){
                        System.out.println("输入错误");
                        continue;
                    }
                    if (dbFind.modifyKey(username,newKey)) {
                        System.out.println("修改成功,修改后的信息为");
                        dbFind.getSelfInfo(username);
                        break;
                    }else {
                        System.out.println("修改失败");
                        break;
                    }

                }
                stack.pop();
                current = stack.peek();
            }else if (current == 3) {
                //修改年龄
                while (true) {
                    System.out.println("输入您要修改的年龄：");
                    String newAge = sc.nextLine();
                    if (newAge.isEmpty()){
                        System.out.println("输入错误");
                        continue;
                    }
                    if(!DataEffect.isNumber(newAge)){
                        System.out.println("输入错误，请输入数字");
                        continue;
                    }
                    if (dbFind.modifyAge(username,newAge)) {
                        System.out.println("修改成功,修改后的信息为");
                        System.out.println(dbFind.getSelfInfo(username));
                        break;
                    }else {
                        System.out.println("修改失败");
                        break;
                    }

                }
                stack.pop();
                current = stack.peek();
            }else if (current == 4) {
                //修改性别
                while (true) {
                    System.out.println("输入您要修改的性别：");
                    String newGender = sc.nextLine();
                    if (newGender.isEmpty() || !(newGender.equals("男") || newGender.equals("女"))){
                        System.out.println("输入错误");
                        continue;
                    }
                    if (dbFind.modifyGnder(username,newGender)) {
                        System.out.println("修改成功,修改后的信息为");
                        System.out.println(dbFind.getSelfInfo(username));
                        break;
                    }else {
                        System.out.println("修改失败");
                        break;
                    }

                }
                stack.pop();
                current = stack.peek();
            }
        }

    }
}